package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class GetValueAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String action=req.getParameter("action");
////        if (action.equals("getvalue")){
////            String[] str =getValue();
////            JSONArray jsonArray=JSONArray.fromObject(str);
////            resp.getWriter().println(jsonArray.toString());
////        }
        ajax(req, resp);
    }

//    public String[] getValue(){
//        String[] str=new String[4];
//        str[0]="java";
//        str[1]="nodejs";
//        str[2]="es6" ;
//        str[3]="angu";
//        return str;
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    protected void ajax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GuestbookService service =new GuestbookServiceImpl();
        List<Object> list = service.queryInfo();
        //获取键
        String username = req.getParameter("username");
        String msg=null;
        for (int i=0;i<list.size();i++){
            Guestbook gb = (Guestbook) list.get(i);
            String name = gb.getName();
            if (name.equals(username)){
                msg="<font color='red'>重新输入用户名</font>";
                resp.getWriter().println(msg);
                break;
            }
        }
        if (msg==null){
            msg="<font color='green'>可以使用</font>";
            resp.getWriter().println(msg);
        }

    }
}
